
package engine.newUi
{
	import engine.control.ControlCreater;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.vo.BattleVo;
	import engine.display.newAttack.vo.WarfareVo;
	import engine.logic.LogSendLogic;
	import engine.logic.MenuListManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.window.NxWindowEvent;

	/**
	 * 阵营战逻辑
	 * @author liuel
	 * 
	 */	
	public class CampBattleLogic
	{
		private static var _self				:CampBattleLogic;
		private		 var _ui				:CampBattleUI;
		private		 var _model				:ModelLocator;
		//阵营战剩余时间
		private 		 var _date				:Date;
		private 		 var _time				:Timer;
		//存放所有战报的数组
        private		 var _allbattleArr		:Vector.<WarfareVo>;
		//存放自己战报数组
		private		 var _selfbattleArr		:Vector.<WarfareVo>;
		private 		 var _picMC				:MovieClip;
		
		//左阵营玩家
		private 		 var _leftuserarr		:Vector.<BattleVo>;
		//右阵营玩家
		private 		 var _rightuserarr		:Vector.<BattleVo>;
		
		private 		 var _leftTabType		:int;
		private 		 var _rightTabType		:int;
		//当前查看战报的类型
		private		 var _warfareType       :int;
		//当前鼓舞的等级
		private 		 var _currEncourageLv  :int=0;
		
		private 		 var _bindIndex			:int =-1; 
		private 		 var _reamdIndex		:int = -1;
		/**
		 * UPDATE 更新或添加
		 * DELETE 删除
		 */		
		private 		const UPDATE:int =1,DELETE:int=2;
		private var	_bingManage			:BindManage;
		/**自动参战状态**/
		private const AUTO_JOIN:int = 1;
		/**是否在阵营战活动中**/
		private var _isJoinCampBattle:Boolean=false;;
		public static function getInstance():CampBattleLogic
		{
			if(_self == null)
			{
				_self = new CampBattleLogic();
				
			}
			return _self;
		}
		public function CampBattleLogic()
		{
		}
		public function Init():void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			userdata.data.state =1;
			_ui		=	CampBattleUI.getInstance();
			_bingManage =BindManage.getInstance();
		
			_date	=   new Date();
			_time	=	new Timer(1000);
			_model	= 	ModelLocator.getInstance();
			initGroup(_ui.leftUserGroup);
			initGroup(_ui.rightUserGroup);
			initUI();
			initTab();
			_model	.	addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_BATTLE_INFO),onOpen);
			_ui.battleTxt.autoScroll=true;
		}
		private function setBtnSate():void
		{
			var vipLv:int = _model.scene.Role.getVipLevel();
			var obj:Object = StaticIni.getIniObj("CampBattleUI_VIP");
			if(obj == null)
			{
//				trace("没有找到配置文件");
				return;
			}
			var needLv:int = int(obj["Excite_VIPLevel"]);
			if(vipLv>=needLv)
			{
				_ui.taelEncourageBtn.visible =true;
				_ui.jionLabel.visible =true;
				_ui.ckeckJoin.visible=true;
			}else
			{
				_ui.taelEncourageBtn.visible =false;
				_ui.jionLabel.visible =false;
				_ui.ckeckJoin.visible =false;
			}
		}
		private function initUI():void
		{
			_ui.leftScroll.addView(_ui.leftUserGroup);
			_ui.leftUserGroup.x=_ui.leftUserGroup.y=0
			_ui.rightScroll.addView(_ui.rightUserGroup);
			_ui.rightScroll.width =250;
			_ui.leftScroll.width =250;
			_ui.leftScroll.vscroll = 0;
			_ui.rightScroll.vscroll= 0;
			
			_allbattleArr		=new Vector.<WarfareVo>();
			_selfbattleArr		=new Vector.<WarfareVo>();
			
		}
		private function setCeckState():void
		{
			var localSO			:SharedObject = 	SharedObject.getLocal( "userdata" );
//			if(localSO.data.state == null)
//			{
//				//第一次打开
//				_ui.ckeckJoin.gotoAndStop(1);
//				return;
//			}	
//			if(localSO.data.state == 1)
//			{
//				_ui.ckeckJoin.gotoAndStop(1);
//			}
//			else if(localSO.data.state == 2)
//			{
//				_ui.ckeckJoin.gotoAndStop(2);
//			}
			//战报选择
			if(localSO.data.lookState ==null)
			{
				//没有选择过,默认全部
				localSO.data.lookState=1;
				
			}
			if(localSO.data.lookState == 1)
			{
				_warfareType=0;
			}
			else if(localSO.data.lookState == 2)
			{
				_warfareType=1;
			}
			
			
		}
		/**
		 *是否在阵营战活动中 
		 * @return 
		 * 
		 */		
		public function get isJoinCamp():Boolean
		{
			return _isJoinCampBattle;
		}
		private function initTab():void
		{
			_ui.leftTab.addItem(StaticIni.getSwfLangStr("UIString_1000071"));
			_ui.leftTab.addItem(StaticIni.getSwfLangStr("UIString_1000072"));
			_ui.rightTab.addItem(StaticIni.getSwfLangStr("UIString_1000071"));
			_ui.rightTab.addItem(StaticIni.getSwfLangStr("UIString_1000072"));
			
		}
		private function bind():void
		{
			if(_bindIndex == -1)
			{
				_bindIndex =_bingManage.BindProp(_model.scene.Role,"ExciteTimes",this,"getExciteTimes");
			}
			if(_reamdIndex ==-1)
			{
				_reamdIndex =_bingManage.BindProp(_model.scene.Role,"AutoJoinCampBattle",this,"getRanmandState");
			}
		}
		private function unBind():void
		{
			if(_bindIndex != -1)
			{
				_bindIndex=_bingManage.UnbindProp(_model.scene.Role,"ExciteTimes",_bindIndex);
			}
			if(_reamdIndex !=-1)
			{
				_reamdIndex=_bingManage.UnbindProp(_model.scene.Role,"AutoJoinCampBattle",_reamdIndex);
			}
		}
		private function initGroup(group:NxViewGroup):void
		{
			var lenth:int 	=	group.col*group.row;
			for(var i:int=0;i<lenth;i++)
			{
				_picMC = group.getItemByIndex(i);
				_picMC["titleTxt2"].text ="";
			}
		}
	
		public function BeforeShow():void
		{
			_leftuserarr = new Vector.<BattleVo>();
			_rightuserarr = new Vector.<BattleVo>();
			if(ActivitiesUI.getInstance().created)
			{
				ActivitiesUI.getInstance().hide();
			}
			
			bind();
			setCeckState();
			setBtnSate();
			_ui.joinBattleBtn.label = StaticIni.getSwfLangStr("UIString_1000066");//参战
//			_rightTabType = 0;
//			_leftTabType  = 0;
			gotoAndPlayMC();
			addEvent();
			
//			if(_model.scene.Role.GetProperties("AutoJoinCampBattle") ==AUTO_JOIN)
//			{
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_JOIN_CAMP_BATTLE);
//			}
			
		
		}
		public function AfterHide():void
		{
			removeEvent();
			unBind();
		}
		private function addEvent():void
		{
			_time					.addEventListener(TimerEvent.TIMER,onPlayTime);
			_ui.windowBG			.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.experEncourageBtn	.addEventListener(MouseEvent.CLICK,onClickExperEncourage);
			_ui.taelEncourageBtn	.addEventListener(MouseEvent.CLICK,onClickTatelEncourage);
			_ui.joinBattleBtn		.addEventListener(MouseEvent.CLICK,onClickJoinBattle);
			_ui.leftTab				.addEventListener(MouseEvent.CLICK,onClickLeftTab);
			_ui.rightTab			.addEventListener(MouseEvent.CLICK,onClickRightTab);
			_ui.allBattleBtn		.addEventListener(MouseEvent.CLICK,onClickAllBattle);
			_ui.selfBattelBtn		.addEventListener(MouseEvent.CLICK,onClickSelfBattle);
			_ui.experEncourageBtn	.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.taelEncourageBtn	.addEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.experEncourageBtn	.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.taelEncourageBtn	.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			
			_ui.ckeckJoin			.addEventListener(MouseEvent.CLICK,onClickJion);
			
			_ui.battleTxt.addLinkClickHandel(backfun);
			_ui.winLabel.addLinkClickHandel(backfun);
		}
		private function removeEvent():void
		{
			_time					.removeEventListener(TimerEvent.TIMER,onPlayTime);
			_ui.windowBG			.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.experEncourageBtn	.removeEventListener(MouseEvent.CLICK,onClickExperEncourage);
			_ui.taelEncourageBtn	.removeEventListener(MouseEvent.CLICK,onClickTatelEncourage);
			_ui.joinBattleBtn		.removeEventListener(MouseEvent.CLICK,onClickJoinBattle);
			_ui.leftTab				.removeEventListener(MouseEvent.CLICK,onClickLeftTab);
			_ui.rightTab			.removeEventListener(MouseEvent.CLICK,onClickRightTab);
			_ui.allBattleBtn		.removeEventListener(MouseEvent.CLICK,onClickAllBattle);
			_ui.selfBattelBtn		.removeEventListener(MouseEvent.CLICK,onClickSelfBattle);
			_ui.experEncourageBtn	.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.taelEncourageBtn	.removeEventListener(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.experEncourageBtn	.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.taelEncourageBtn	.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			
			_ui.ckeckJoin			.removeEventListener(MouseEvent.CLICK,onClickJion);
			_ui.battleTxt.removeLinkClickHandel();
			_ui.winLabel.removeLinkClickHandel();
		}
		private function backfun(val:String):void
		{
			if(val !="" && val !=_model.scene.Role.getPlayerName())
					MenuListManager.clickPkListName(val);
		}
		/**
		 *绑定 是否自动参展 
		 * @param val
		 * 
		 */		
		public function set getRanmandState(val:int):void
		{
			CurrencyResultLogic.getInstance().AutoLeave =(val==1)?true:false;
			_ui.ckeckJoin.gotoAndStop(val+1);
			AutoJoin();
		}
		public function set getExciteTimes(val:int):void
		{
			if(val==0)
			{
				_ui.encourageLabel.visible =false;
			}
			else
			{
				_ui.encourageLabel.visible =true;
			}
			_currEncourageLv=val;
			if(_currEncourageLv >=5)
			{
				_ui.experEncourageBtn.enabled	=	false;
				_ui.taelEncourageBtn.enabled 	=	false;
			}
			else
			{
				_ui.experEncourageBtn.enabled	=	true;
				_ui.taelEncourageBtn.enabled 	=	true;
			}
		
			_ui.encourageLabel.htmlText=StaticIni.getSwfLangStrVar("UIString_1000051",[_currEncourageLv*20])+"";
			
			
		}

		private function onClickJion(e:MouseEvent):void
		{
//			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			if(_ui.ckeckJoin.currentFrame == 1)
			{
//				userdata.data.state =2;
//				_ui.ckeckJoin.gotoAndStop(2);
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SET_AUTO_JOIN_CAMPBATTLE,1);
			}
			else
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SET_AUTO_JOIN_CAMPBATTLE,0);
//				userdata.data.state =1;
//				_ui.ckeckJoin.gotoAndStop(1);
			}
//			AutoJoin();
			
		}
		/**
		 *自动参展 
		 * 
		 */		
		private function AutoJoin():void
		{
//			if(_ui.ckeckJoin.currentFrame == 2)
//			{
//				if(_ui.joinBattleBtn.label == StaticIni.getSwfLangStr("UIString_1000066"))
//				{
//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_JOIN_CAMP_BATTLE);
//				}
//			}
		}
		/**
		 * 点击左Tab 
		 * @param e
		 * 
		 */		
		private function onClickLeftTab(e:MouseEvent):void
		{
			_leftTabType = _ui.leftTab.selected
			showTabInfo(_leftTabType,_ui.leftUserGroup,_leftuserarr);
		}
		/**
		 * 点击右Tab 
		 * @param e
		 * 
		 */		
		private function onClickRightTab(e:MouseEvent):void
		{
			_rightTabType=_ui.rightTab.selected;
			showTabInfo(_rightTabType,_ui.rightUserGroup,_rightuserarr);
		}
		/**
		 * 根据当前TabIndex 显示相应数据 
		 * @param va  Tab index
		 * 
		 */		
		private function showTabInfo(va:int,group:NxViewGroup,arr:Vector.<BattleVo>):void
		{
			switch(va)
			{
				case 0:
					restGroup(group,setTab(20,49,arr));
					break;
				case 1:
					restGroup(group,setTab(50,100,arr));
					break;
			}
		}
		/**
		 * 根据等级 设置阶段等级的用户 
		 * @param minlv  开始等级
		 * @param maxlv  结束等级
		 * @param arr    阵营用户
		 * @return       所在阵营等级阶段的用户
		 * 
		 */		
		private function setTab(minlv:int,maxlv:int,userArr:Vector.<BattleVo>):Vector.<BattleVo>
		{
			var arr:Vector.<BattleVo> = new Vector.<BattleVo>();
			for(var i:String in userArr)
			{
				if(userArr[i].level>=minlv &&userArr[i].level<=maxlv)
				{
					arr.push(userArr[i]);
				}
			}
			return arr;
		}
		
		/**
		 * 重置Group 
		 * @param group   对象
		 * @param userarr 用户数组
		 * 
		 */		
		private function restGroup(group:NxViewGroup,userarr:Vector.<BattleVo>):void
		{

			if(userarr.length != group.row)
			{
				var skin:String=group.viewSkin;
			    var col:int=group.col;
				var row:int=userarr.length;
				var pa:DisplayObjectContainer=group.parent;
				if(group == _ui.leftUserGroup)
				{
					pa.removeChild(group)
					_ui.leftScroll.removeView(group);
					group=null;
					//创建
					_ui.leftUserGroup=ControlCreater.getInstance().CreateNxViewGroup(row,1,skin);
					group =_ui.leftUserGroup;
				}
				else
				{
					pa.removeChild(group)
					_ui.rightScroll.removeView(group);	
					group=null;
					//创建
					_ui.rightUserGroup=ControlCreater.getInstance().CreateNxViewGroup(row,1,skin);
					group =_ui.rightUserGroup;
				}
				pa.addChild(group);
				updateGroup(group,userarr);
			    if(group == _ui.leftUserGroup)
				{
					_ui.leftScroll.addView(group);
				}
				else
				{
				   _ui.rightScroll.addView(group);	
				}
			}
			else
			{
				updateGroup(group,userarr);
			}
		}
		/**
		 * 更新阵营状态 
		 * @param val
		 * @param val1
		 * 
		 */		
		private function updateMc(val:int,val1:int):void
		{
			_ui.leftstate	.visible	=	false;
			_ui.rightstate	.visible	=	false;
			if(val>val1)
			{
				_ui.leftstate.visible	=	true;
			}
			else if(val <val1)
			{
				_ui.rightstate.visible	=	true;
			}
		}
		/**
		 * 更新Group  
		 * @param group
		 * @param arr    
		 * 
		 */		
		private function updateGroup(group:NxViewGroup,arr:Vector.<BattleVo>):void
		{
			var lenth:int 	=	group.col*group.row;
			for(var i:int=0;i<lenth;i++)
			{
				_picMC = group.getItemByIndex(i);
				var str:String =byValshowStr(arr[i]);
			   _picMC["titleTxt2"].htmlText =str;
			   if(group ==_ui.leftUserGroup )
			   {
				   _picMC["img"].gotoAndStop(3);
			   }
			   else
			   {
				   _picMC["img"].gotoAndStop(2);
			   }
			}
			
			
		}
		/**
		 * 根据玩家阵营显示不同颜色的str 
		 * @param vo  玩家
		 * return     字符串
		 */		
		private function byValshowStr(vo:BattleVo):String
		{
			
			var strID:String
			if(vo.camp ==1 && vo.state == 1)    
			{
				strID = "UIString_1000056"
			}
			else if(vo.camp == 1 && vo.state == 2) 
			{
				strID = "UIString_1000055"
			}
			else if(vo.camp == 2 && vo.state == 1)
			{
				strID = "UIString_1000058";
			}
			else 
			{
				strID ="UIString_1000057";
			}
			var str:String = StaticIni.getSwfLangStrVar(strID,[vo.name]);
			return str;
		}
		private function clearGroup(group:NxViewGroup):void
		{
			var lenth:int 	=	group.col*group.row;
			for(var i:int=0;i<lenth;i++)
			{
				_picMC = group.getItemByIndex(i);
				_picMC["titleTxt2"].text ="";
			}
		}
		private function onOpen(e:CustomMsgEvent=null):void
		{
			var arr:Array =[];
			arr=e.pList;
			
			var index:int = arr.shift();
	
		
			if(!_ui.created &&(index !=4||index !=5||index !=6))
			{
				switchTab();
				_ui.showOrHide();
			}
	
//			if(index >3 && index<7 ) //战报
//			{
//				
//			}
//			else
//			{
			_ui.visible	= !AttackLogic.getInstance().fighting;
//			}
			switch(index)
			{
				//个人信息
				case 1:
					updateSelfBattleInfo(arr);
					break;
				//阵营玩家信息
				case 2:
					saveUser(arr);
					break;
				//阵营战积分等信息
				case 3:
					updateTopInfo(arr);
					break;
				//鼓舞状态
				case 7:
					break;
				//参战成功
				case 8:
					_ui.joinBattleBtn.label = StaticIni.getSwfLangStr("UIString_1000067");
					_isJoinCampBattle=true;
					switchTab();
					break;
				//退出战斗
				case 9:
					_ui.joinBattleBtn.label = StaticIni.getSwfLangStr("UIString_1000066");
					_isJoinCampBattle=false;
					AutoJoin();
//					var localSO			:SharedObject = 	SharedObject.getLocal( "userdata" );
//					if(localSO.data.state &&localSO.data.state ==2 && _ui.ckeckJoin.currentFrame == 2)
//					{
//						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_JOIN_CAMP_BATTLE);
//					}
					break;
				//阵营战结束
				case 10:
					_ui.hide();
					CurrencyResultLogic.getInstance().AutoLeave=false;
					_isJoinCampBattle=false;
					_allbattleArr=new Vector.<WarfareVo>();
					_selfbattleArr=new Vector.<WarfareVo>();
					_ui.battleTxt.htmlText="";
					AlertUI.showAlert(StaticIni.getSwfLangStr("UIString_1000084"),StaticIni.getSwfLangStr("UIString_1000329"),AlertUI.OK,clickFunc);
					break;
				//战报
				default:
					var vo:WarfareVo =getWarfare(index,arr);
					saveAllBattle(vo);
					byTypeShowWarfare(vo);
					break
			}
			
			AutoJoin();
//			if(	TopPlayerLogic.getInstance().autoState==true && _isJoinCampBattle ==false )
//			{
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_JOIN_CAMP_BATTLE);
//			}

		}
		private function clickFunc(clickName:uint,inputTxt:String,clickData:Array):void
		{
			if(clickName==AlertUI.OK)
			{
				AlertUI.hideAlert();
			}
		}
		/**
		 * 更新自己战况信息 
		 * @param arr
		 * 
		 */		
		private function updateSelfBattleInfo(arr:Array):void
		{
			if(arr.length>5)
			{
				//d昂前胜场
			  _ui.currbattle.htmlText = StaticIni.getSwfLangStrVar("UIString_1000045",[arr[1],arr[3]])+"";
			  //声望
			  _ui.prestigeLabel.htmlText = StaticIni.getSwfLangStrVar("UIString_1000046",[arr[5]])+"";
			  //最高胜场
		      _ui.maxbattle .htmlText = StaticIni.getSwfLangStrVar("UIString_1000044",[arr[2],arr[0]])+"";	
			  //金币
			  _ui.goldLabel .htmlText = StaticIni.getSwfLangStrVar("UIString_1000047",[arr[4]])+"";
			}
		}
		/**
		 * 更新阵营Top信息 
		 * @param arr
		 * 
		 */	
		private function updateTopInfo(arr:Array):void
		{
			if(arr == null)
				return;
			var time:int =arr.shift();
			_date.time = time*1000;
			if(_date.time>0)
			{
				_time.start();
			}
			var leftval		:int 		=arr.shift();
			var rightval	:int		=arr.shift();
			_ui.leftLabel	.htmlText	=StaticIni.getSwfLangStrVar("UIString_1000059",[leftval]);
			
			_ui.rightLabel	.htmlText	=StaticIni.getSwfLangStrVar("UIString_1000059",[rightval]);
			updateMc(leftval,rightval);
			//最大连胜数
			var colorStr:String;
			if(arr.shift() ==1)  //左阵营玩家
			{
				colorStr = StaticIni.getSwfLangStrVar("UIString_1000053",arr);
			}
			else    //右阵营玩家
			{
				colorStr = StaticIni.getSwfLangStrVar("UIString_1000060",arr);
			}
			arr[1] >=2?_ui.winLabel.visible=true:_ui.winLabel.visible=false;
			_ui.winLabel.htmlText=StaticIni.getSwfLangStr("UIString_1000052")+colorStr;
		}
		/**
		 * 点击所有战报 
		 * @param e
		 * 
		 */		
		private function onClickAllBattle(e:MouseEvent):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			userdata.data.lookState =1;
			_warfareType=0;
			gotoAndPlayMC();
			showAllBattle();
		}
		/**
		 * 点击个人战报 
		 * @param e
		 * 
		 */		
		private function onClickSelfBattle(e:MouseEvent):void
		{
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			userdata.data.lookState =2;
			_warfareType=1;
			gotoAndPlayMC();
			showSelfBattle();
		}
		/**
		 * 点击阅历鼓舞 
		 * @param e
		 * 
		 */		
		private function onClickExperEncourage(e:MouseEvent):void
		{
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CAMP_BATTLE_EXCITE,2);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,1,1);

		}
		/**
		 *点击银两鼓舞 
		 * @param e
		 * 
		 */		
		private function onClickTatelEncourage(e:MouseEvent):void
		{
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CAMP_BATTLE_EXCITE,1);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,1,4);
			LogSendLogic.getInstance().btnClickNumSend(22830);

		}
		/**
		 * 点击参战或者退出 
		 * @param e
		 * 
		 */		
		private function onClickJoinBattle(e:MouseEvent):void
		{
			var str:String = StaticIni.getSwfLangStr("UIString_1000066");//参战
			if(_ui.joinBattleBtn.label == str)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_JOIN_CAMP_BATTLE);
			}
			else
			{
				var userdata:SharedObject	=	SharedObject.getLocal("userdata");
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CANCEL_CAMP_BATTLE);
				_ui.ckeckJoin.gotoAndStop(1);
				userdata.data.state =1;
				CurrencyResultLogic.getInstance().AutoLeave=false;
			}
			if(_ui.ckeckJoin.currentFrame==1)
			{
				CurrencyResultLogic.getInstance().AutoLeave=false;
			}
			else
			{
				CurrencyResultLogic.getInstance().AutoLeave=true;
			}
		}
		private function onPlayTime(e:TimerEvent):void
		{
			_date.time	-=	1000;
			var arr:Array=getTimeStr(_date);
			_ui.tiemLabel.htmlText	=StaticIni.getSwfLangStrVar("UIString_1000054",arr)+"";
			if(_date.time<=0)
			{
				_time.reset();
//				_ui.hide();
			}
		}
		/**
		 * 存储并更阵营玩家 
		 * @param arr
		 * 
		 */		
		private function saveUser(arr:Array):void
		{
			// 1增加修改 2删除
			var type:int = arr.shift();
			//左方人数
			var leCount:int = arr.shift();
			var leftarr:Array=[];

			if(leCount>0)
			{
				for(var i:int=0;i<leCount*4;i++)
				{
					leftarr.push(arr.shift());
				}
					
				_leftuserarr=getUser(leftarr,_leftuserarr,type);
					
			}	
			showTabInfo(_leftTabType,_ui.leftUserGroup,_leftuserarr);
			var riCount:int = arr.shift();
			var rightarr:Array =[];
			if(riCount>0)
			{
				for(var j:int=0;j<riCount*4;j++)
				{
					rightarr.push(arr.shift());
				}
				_rightuserarr=getUser(rightarr,_rightuserarr,type);
			}
			showTabInfo(_rightTabType,_ui.rightUserGroup,_rightuserarr);
		}
		/**
		 * 更新阵营阵营玩家 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function getUser(arr:Array,vector:Vector.<BattleVo>,type:int):Vector.<BattleVo>
		{
			var battle:BattleVo; 
			for( var i : int = 0; i < arr.length; i+=4 ) 
			{
				battle=new BattleVo( arr[ i ],arr[i+ 1],arr[i+2],arr[i+3] );
				var index:int=getPos(battle.name);
				if(index ==-1)
				{
					if(type==UPDATE)
						vector.push(battle)
				}
				else
				{
					if(type==UPDATE)
					{
						vector[index]=battle
					}
					else
					{ 
						if(vector.length>index)
							vector.splice(int(index),1);
					}
				}
			}
			function getPos(name:String):int
			{
				for(var j:String in vector)
				{
					if(vector[j].name == name)
					{
						return int(j);
					}
				}
				return -1;
			}
			return vector
		}
		/**
		 * 显示所有战报 
		 * 
		 */		
		private function showAllBattle():void
		{
			_ui.battleTxt.htmlText	="";
			if(_allbattleArr == null)
				return;
			
			for(var i:String in _allbattleArr)
			{
				_ui.battleTxt.htmlText += setStr(_allbattleArr[i]) +"<br>";
				
			}
		}
		
		/**
		 * 显示自己战报 
		 * 
		 */		
		private function showSelfBattle():void
		{
			_ui.battleTxt.htmlText	="";
			if(_selfbattleArr == null)
				return;
			for(var i:String in _selfbattleArr)
			{
				_ui.battleTxt.htmlText +=setStr(_selfbattleArr[i])+"<br>";
			}
		}
		private function gotoAndPlayMC():void
		{
			switch(_warfareType)
			{
				case 0:
					_ui.allBattleBtn.gotoAndStop(2)
					_ui.selfBattelBtn.gotoAndStop(1);
					showAllBattle();
					break;
				case 1:
					_ui.allBattleBtn.gotoAndStop(1)
					_ui.selfBattelBtn.gotoAndStop(2);
					showSelfBattle();
				   break;
			}
		}
		/**
		 * 根据类型显示战报 
		 * @param type  战报
		 */		
		private function byTypeShowWarfare(vo:WarfareVo):void
		{
			if(vo == null)
				return;
			switch(_warfareType)
			{
				case 0:
					showAllBattle();
//					_ui.battleTxt.htmlText += setStr(vo)+"<br>";
					break;
				case 1:
//					if(_model.scene.Role.getPlayerName()==vo.name)
//					{
					showSelfBattle();
//						_ui.battleTxt.htmlText += setStr(vo)+"<br>";
//					}
					break;
			}
		}
		/**
		 *检查获取的声望 
		 * @return 
		 * 
		 */		
		private function getReputationIsFull(arr:Array):Boolean
		{
			var reputation:int =arr[arr.length-1];
			return reputation==0?true:false;
		}
		/**
		 * 根据战报类型设置字符串颜色 
		 * @param vo
		 * @return 
		 * 
		 */		
		public function setStr(vo:WarfareVo):String
		{
			if(vo == null)
				return "";
			
			var strID:String
			var isFull:Boolean =getReputationIsFull(vo.val);
			switch(vo.type)
			{
				//普通战报
				case 4:
					// =1 左阵营 =2右阵营
					if(isFull)
					{
						vo.camp == 1?strID="UIString_1000478":strID="UIString_1000481";
					}
					else
					{
						vo.camp == 1?strID="UIString_1000048":strID="UIString_1000061";
					}
				
					break;
				//连胜
				case 5:
					if(isFull)
					{
						vo.camp == 1?strID="UIString_1000479":strID="UIString_1000482";
					}
					else
					{
						vo.camp == 1?strID="UIString_1000049":strID="UIString_1000062";
					}
				
					break;
				//终结
				case 6:
					if(isFull)
					{
						vo.camp == 1?strID="UIString_1000480":strID="UIString_1000483";
					}
					else
					{
						vo.camp == 1?strID="UIString_1000050":strID="UIString_1000063";
					}
				
					break;
				//轮空奖励 vo.val [名字，铜钱]
				case 11:
						vo.camp == 1?strID="UIString_1000511":strID="UIString_1000512";
					break;
			}
			var str:String = StaticIni.getSwfLangStrVar(strID,vo.val)
			return str;
		
		}
		/**
		 * 转化一条战报 
		 * @param type  战报类型
		 * @param arr  名字+ 战报数据 
		 * @return 
		 * 
		 */			
		public function getWarfare(type:int,arr:Array):WarfareVo
		{
			if(arr ==null)
				return null;
			var camp:int =arr.shift(); //阵营舍弃
			var name:String =arr[0];	//胜利的名字
			var warvo:WarfareVo;
			
			if(type!=11)
			{
				var name1:String =arr[1]; //失败名字
				warvo=new WarfareVo( camp,name,name1,type,arr)
			}
			else
			{
				warvo=new WarfareVo( camp,name,"",type,arr);
			}
		
			return warvo
		}
		/**
		 *存取所有战报 
		 * 
		 */		
		private function saveAllBattle(warvo:WarfareVo):void
		{
			if(_allbattleArr.length>30)
			{
				_allbattleArr.splice(0,_allbattleArr.length-30);
			}
			_allbattleArr.push(warvo);
			saveSelfBattle(warvo);
		}
		/**
		 *超找玩家自己所在页签 
		 * 
		 * 
		 */		
		private function switchTab():void
		{
			 var camp:int  = _model.scene.Role.GetProperties("TempCamp");
			
		     var level:int = _model.scene.Role.getPlayerLevel();
			
				if(level>=20 && level<=49)   //做 0  
				{
//					if(camp == 1)
//					{
						_ui.leftTab.selected = 0;
						_leftTabType =0;
						_ui.rightTab.selected =0;
						_rightTabType =0;
//					}
//					else
//					{
//						_ui.rightTab.selected =0;
//						_rightTabType =0;
//					}
				}
				else if(level>=50 )  // zuo 1
				{
//					if(camp == 1)
//					{
						_ui.leftTab.selected =1;
						_leftTabType =1
//					}
//					else
//					{
						_ui.rightTab.selected =1;
						_rightTabType =1
//					}
				}


		}
		/**
		 * 存自己的战报 
		 * @param arr 战报数据
		 * @return 
		 * 
		 */		
		private function saveSelfBattle(warvo:WarfareVo):void
		{
			if(_selfbattleArr.length>30)
			{
				_selfbattleArr.splice(0,_selfbattleArr.length-30);
			}
			if(_model.scene.Role.getPlayerName()==warvo.name ||_model.scene.Role.getPlayerName()== warvo.name1 )
			{
				_selfbattleArr.push(warvo);
			}
		}
		/**
		 * 格式时间 
		 * @param val
		 * @return 
		 * 
		 */		
		private function initTime(val:int):String
		{
			var  va:String = val.toString();
			val>9?va:va="0"+va;
			return va;
		}
		/**
		 * 转换时间 
		 * @param _date
		 * @return 
		 * 
		 */		
		private function getTimeStr(_date:Date):Array
		{
			var minutes:int  = _date.minutesUTC;
			var seconds:int  = _date.secondsUTC;
			var arr:Array   = [initTime(minutes),initTime(seconds)];
			return arr;	
		}
		/**
		 * 点击关闭 退出阵营战 
		 * @param e
		 * 
		 */		
		private function onClose(e:NxWindowEvent):void
		{
			_isJoinCampBattle=false;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLOSE_CAMP_BATTLE_UI);
			_ui.hide();
		}
		
		private function onShowTips(e:MouseEvent):void
		{
			var strID:String;
			switch(e.currentTarget)
			{
				case _ui.experEncourageBtn:
					strID = "UIString_1000064"
					break;
				 case _ui.taelEncourageBtn:
					 strID = "UIString_1000065"
					 break;
			}
			var str:String = StaticIni.getSwfLangStr(strID);
			TipsMainLogic.instance.showString(str);
		}
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
	}
}